// xc_lshop/distr/manage/leader/statistics.js
var actbill = require('../../model/distribution_team.js')
var wxCharts = require('../../../../utils/wxcharts');
Page({
  mixins: [require('../../../../xc/mixins/basicMixin')],

  /**
   * 页面的初始数据
   */
  data: {
    nav:[],
    active:"money",
  },
  xcloaddata: function (options, config) {
    if(config.payment){
      this.setData({
        paymentconfig:config.payment
      })
    }
    // this.setconfig(config)
    this.gettimlist(1)
   
    // this.makeareaChart()
  },
  makeringchart(xcmodel,windowWidth){
   
    var totalbalancepay=xcmodel.curmonth.totalbalancepay
    // totalbalancepay=totalbalancepay.toFixed(0)
    var onlinepay=xcmodel.curmonth.onlinepay
    // onlinepay=onlinepay.toFixed(0)
    // console.log("toFixed")

    // console.log(onlinepay)
    // console.log(totalbalancepay)
    var paymentconfig=this.data.paymentconfig
    var  ringChart = new wxCharts({
      canvasId: 'ringCanvas',
      type: 'ring',
      width: windowWidth,
      height: 200,
      series: [{
       name: '微信支付',
       data: onlinepay,
      }, {
       name: (paymentconfig.balancepayname?paymentconfig.balancepayname:'余额') + "支付",
       data: totalbalancepay,
      }],
      dataLabel: true
     });
  },
  makechat(xcmodel){
    var windowWidth = 320;
    try {
        var res = wx.getSystemInfoSync();
        windowWidth = res.windowWidth - 15;
    } catch (e) {
        console.error('getSystemInfoSync failed!');
    }

    this.makeringchart(xcmodel,windowWidth)
    this.makeareaChart(xcmodel,windowWidth)
  },
  makeareaChart(xcmodel,windowWidth){
    this.areaChart("numsareaCanvas",xcmodel['qushi']['numsareaCanvas'],xcmodel['qushi']['categories'],windowWidth)
    this.areaChart("peopleareaCanvas",xcmodel['qushi']['peopleareaCanvas'],xcmodel['qushi']['categories'],windowWidth)
    this.areaChart("priceareaCanvas",xcmodel['qushi']['priceareaCanvas'],xcmodel['qushi']['categories'],windowWidth)

  },
  areaChart(key,obj,categories,windowWidth){
    new wxCharts({
      canvasId:key,
      type: 'area',
      categories: categories,
      series: [{
       name: obj['name'],
       data:  obj['data'],
       format: function (val) {
        return val + obj['unit'];
       }
      }],
      yAxis: {
       format: function (val) {
        return val +obj['unit'];
       }
      },
      width: windowWidth,
      height: 300
     });
  },
  
  onlicknav(e){
    var id=e.currentTarget.dataset.id
    this.setData({
      curid:id
    })
    this.getstaticsdata()
 },
  loadinglist(){
    var page= this.data.page + 1;
    var navdata= this.data.navdata;
    console.log(navdata)
    this.gettimlist(page,5,navdata)
  },
  gettimlist(page,limit=5,data=null){
    var date=new Date();
    var nowmonth="";
    var nowyear="";
    if(page==1){
      nowmonth=date.getMonth()+1;
      nowyear=date.getFullYear();
    }else{
      nowmonth=data['month'];
      nowyear=data['year'];
    }
    var list=[]
    for(var i=0;i<limit;i++){
      
      var item={}
      item['name']=nowyear+"年"+nowmonth+"月";
      item['id']=nowyear+"-"+ (nowmonth<10?'0'+nowmonth:nowmonth)
      list.push(item)

      nowmonth=nowmonth - 1;
      if(nowmonth==0){
        nowmonth=12;
        nowyear= nowyear - 1
      }
    }
    var updata={ }
    updata["page"]=page;
    updata["nav"]=this.data.nav.concat(list);
    updata["navdata"]={month:nowmonth,year:nowyear};

    if(page==1){
      updata["curid"]=list[0]['id'];
    }

    this.setData(updata)

    if(page==1){
      this.getstaticsdata()
      // this.getpagelist(1)
    }
    
  },
  getstaticsdata(){
    var that = this
    var condition={}
    condition['date']=this.data.curid
    actbill.getstaticsdata(condition).then(function (res) {
        console.log("res")
        console.log(res)
        if(res['status']==1){
          that.setData({
            xcmodel:res['obj']
          })
          that.makechat(res['obj'])
        }
        

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})